package com.ctg.itrdc.imageconsole.util;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

import com.ctg.itrdc.imageconsole.common.constants.CommonConstant;
import com.ctg.itrdc.imageconsole.dto.SystemUserDTO;

/**
 * @description:
 * @version: v1.0.0
 * @author: FFSC-CHENSS
 *
 */
public class SessionUtils implements HttpSessionListener {

	private SessionUtils() {

	}

	public static SystemUserDTO getSysUser() {
		HttpServletRequest request = RequestUtils.getRequest();
		HttpSession session = request.getSession();
		SystemUserDTO sysUser = (SystemUserDTO) session.getAttribute(CommonConstant.SessionKey.SYSUSER);
		return sysUser;
	}

	@Override
	public void sessionCreated(HttpSessionEvent se) {
		System.out.println("创建session......");
		ServletContext context = se.getSession().getServletContext();
		Integer count = (Integer) context.getAttribute("count");
		if (count == null) {
			count = new Integer(1);
		} else {
			int co = count.intValue();
			count = new Integer(co + 1);
		}
		System.out.println("当前用户人数：" + count);
		context.setAttribute("count", count);// 保存人数
	}

	@Override
	public void sessionDestroyed(HttpSessionEvent se) {
		System.out.println("销毁session......");
		ServletContext context = se.getSession().getServletContext();
		Integer count = (Integer) context.getAttribute("count");
		int co = count.intValue();
		count = new Integer(co - 1);
		context.setAttribute("count", count);
		System.out.println("当前用户人数：" + count);
	}
}
